# =============================================================================
# GOOGLE COLAB SETUP / GOOGLE COLAB SETUP
# =============================================================================
# Sjekk om vi kjører i Google Colab
try:
import google.colab
IN_COLAB = True
print("🔧 Kjører i Google Colab - installerer avhengigheter...")
print("🔧 Running in Google Colab - installing dependencies...")
# Installer nødvendige pakker
import subprocess
import sys
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q",
"networkx", "matplotlib", "plotly", "pydantic",
"pyyaml", "pandas", "ipywidgets", "pillow", "kaleido"])
print("✅ Pakker installert")
except Exception as e:
print(f"⚠️ Pip install feilet: {e}")
# Fjern eksisterende slektstre-mappe hvis den finnes
import shutil
import os
if os.path.exists('/content/slektstre'):
shutil.rmtree('/content/slektstre')
print("🗑️ Fjernet eksisterende slektstre-mappe")
# Klon repository
try:
subprocess.check_call(['git', 'clone', 'https://github.com/arvidl/slektstre.git'])
print("✅ Repository klonet")
except Exception as e:
print(f"⚠️ Git clone feilet: {e}")
# Legg til src-mappen til Python path og importer direkte
sys.path.insert(0, '/content/slektstre/src')
print("✅ Path lagt til")
# Importer slektstre-modulene direkte for å unngå navnekonflikt
import importlib.util
import types
# Først, fjern konfliktende moduler fra sys.modules
modules_to_remove = ['tree', 'models', 'localization']
for module_name in modules_to_remove:
if module_name in sys.modules:
del sys.modules[module_name]
# Last inn models.py først
try:
spec = importlib.util.spec_from_file_location("slektstre_models", "/content/slektstre/src/models.py")
slektstre_models = importlib.util.module_from_spec(spec)
spec.loader.exec_module(slektstre_models)
# Opprett midlertidig models modul
temp_models_module = types.ModuleType('models')
temp_models_module.Person = slektstre_models.Person
temp_models_module.Gender = slektstre_models.Gender
temp_models_module.Ekteskap = slektstre_models.Ekteskap
temp_models_module.FamilieData = slektstre_models.FamilieData
sys.modules['models'] = temp_models_module
print("✅ models.py lastet")
except Exception as e:
print(f"⚠️ models.py feilet: {e}")
# Last inn localization.py
try:
spec = importlib.util.spec_from_file_location("slektstre_localization", "/content/slektstre/src/localization.py")
slektstre_localization = importlib.util.module_from_spec(spec)
spec.loader.exec_module(slektstre_localization)
# Opprett midlertidig localization modul
temp_localization_module = types.ModuleType('localization')
temp_localization_module.t = slektstre_localization.t
sys.modules['localization'] = temp_localization_module
print("✅ localization.py lastet")
except Exception as e:
print(f"⚠️ localization.py feilet: {e}")
# Last inn tree.py som slektstre_tree
try:
spec = importlib.util.spec_from_file_location("slektstre_tree", "/content/slektstre/src/tree.py")
slektstre_tree = importlib.util.module_from_spec(spec)
spec.loader.exec_module(slektstre_tree)
# Opprett midlertidig tree modul
temp_tree_module = types.ModuleType('tree')
temp_tree_module.Slektstre = slektstre_tree.Slektstre
sys.modules['tree'] = temp_tree_module
print("✅ tree.py lastet")
except Exception as e:
print(f"⚠️ tree.py feilet: {e}")
# Last inn family_io.py
try:
spec = importlib.util.spec_from_file_location("slektstre_io", "/content/slektstre/src/family_io.py")
slektstre_io = importlib.util.module_from_spec(spec)
spec.loader.exec_module(slektstre_io)
print("✅ family_io.py lastet")
except Exception as e:
print(f"⚠️ family_io.py feilet: {e}")
# Last inn visualization.py
try:
spec = importlib.util.spec_from_file_location("slektstre_viz", "/content/slektstre/src/visualization.py")
slektstre_viz = importlib.util.module_from_spec(spec)
spec.loader.exec_module(slektstre_viz)
print("✅ visualization.py lastet")
except Exception as e:
print(f"⚠️ visualization.py feilet: {e}")
print("✅ Slektstre-moduler lastet inn i Colab")
except ImportError:
IN_COLAB = False
print("💻 Kjører lokalt / Running locally")
import sys
sys.path.append('../src')
except Exception as e:
print(f"⚠️ Colab setup feilet: {e}")
IN_COLAB = False
print("💻 Fallback til lokal modus / Fallback to local mode")
import sys
sys.path.append('../src')
print(f"📍 Miljø: {'Google Colab' if IN_COLAB else 'Lokal'}")
print(f"📍 Environment: {'Google Colab' if IN_COLAB else 'Local'}")